$(function() {
	//购物车图标跳转
	$('.cart_link').click(function() {
		location.href = '/mobile/cart';
	});

	//返回图标跳转
	$('.back_btn').click(function() {
		location.href = '/mobile/search';
	});

	//点击选择购买数量或者选择加入购物车
	//弹出层锁定在页面中下部，供用户选择数量
	//并允许关闭按钮
	$('.buy_set').click(function() {
		//弹出层		
		$('.set_pop_panel').slideDown('2000');

	});

	//弹出层关闭按钮
	$('.pop_close_btn').click(function() {
		$('.set_pop_panel').slideUp('2000');
	});

	//点击加入购物车
	$('.cart_btn').click(function() {
		$('.set_pop_panel').slideDown('2000');
	});

	//点击加减按钮操作
	//减号
	$('.count_panel input[name=discut]').click(function() {
		var pd_num = parseInt($('input[name=pd_num]').val());
		if(pd_num <= 1) {
			$('input[name=pd_num]').val(1);
		} else {
			$('input[name=pd_num]').val(pd_num - 1);
		}
	});

	//加号操作
	$('.count_panel input[name=add]').click(function() {
		var pd_num = parseInt($('input[name=pd_num]').val());
		var pd_amount = parseInt($('b.pd_amount').text());
		if(pd_num >= pd_amount) {
			$('input[name=pd_num]').val(pd_amount);
		} else {
			$('input[name=pd_num]').val(pd_num + 1);
		}
	});

	//点击弹出层加入购物车按钮
	$('.pop_cart_btn').click(function() {
		buy_fun();
	});
	//弹出层点击立即购买按钮
	$('.pop_buy_btn').click(function() {
		buy_fun();
	});
	//立即购买按钮
	$('.order_btn').click(function() {
		//默认买一个
		buy_fun();
	});

	//通用购买方法
	function buy_fun() {
		var pd_num = parseInt($('input[name=pd_num]').val());
		//alert('立即购买');
		var product_Id = $('input[name=product_Id]').val();
		$.post(
			'/mobile/cart/addhandle', {
				product_Id: product_Id,
				goods_num: pd_num
			},
			function(data, status) {
				if(data.url == '/login') {
					location.href = data.url;
				} else {
					//已经登录则跳转
					location.href = data.url;
				}
			}
		);
	}
});